package com.sx.jys.ui.vm

import com.blankj.utilcode.util.ToastUtils
import com.sx.jys.app.net.HttpRequestCoroutine
import com.sx.jys.app.net.bean.AccountBean
import com.sx.jys.app.until.MutableLiveListData
import com.sx.jys.app.until.request2
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.livedata.BooleanLiveData
import me.hgj.jetpackmvvm.callback.livedata.StringLiveData
import java.math.BigDecimal

class AccountVm : BaseViewModel() {
    val listLiveData = MutableLiveListData<AccountBean>()

    val uiState = UIStateChange()

    inner class UIStateChange {
        val yhTotalField = StringLiveData()
        val refreshEvent = BooleanLiveData()
        val loadMoreEvent = BooleanLiveData()
    }

    fun reqAccountList() {
        request2({ HttpRequestCoroutine.accountList() }, {
            listLiveData.value = it
            var totalCount = BigDecimal.ZERO
            it.forEach {
                totalCount = it.crossUnPnl?.toBigDecimal()?.add(totalCount)
                totalCount = it.totalProfit?.toBigDecimal()?.add(totalCount)
            }
            uiState.yhTotalField.value = totalCount.stripTrailingZeros().toPlainString()
        }, isShowDialog = true)
    }

    fun resetValue(id: String, value: String) {
        request2({ HttpRequestCoroutine.marginLimit(id, value) }, {
            ToastUtils.showShort("设置成功")
            reqAccountList()
        }, isShowDialog = true)
    }

    fun leverageAllLimit(value: String) {
        request2({ HttpRequestCoroutine.leverageAllLimit(value) }, {
            ToastUtils.showShort("设置成功")
            reqAccountList()
        })
    }
}